from django.db import models

# Create your models here.
class Brand(models.Model):
    first_name = models.CharField('品牌首字母',max_length=10)
    name = models.CharField('品牌的名字',max_length=20)
    logo = models.CharField('品牌的logo',max_length=200)

    def  __str__(self):
        return self.name
    class Meta:
        db_table = 'brand'
        verbose_name_plural = '品牌'

class Cate(models.Model):
    name = models.CharField('商品种类',max_length=30)
    cid = models.ForeignKey('self',on_delete=models.CASCADE,
                            related_name='subs',
                            null=True,blank=True)
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'cate'
        verbose_name_plural = '商品种类'

class Group(models.Model):
    name = models.CharField('频道组名字',max_length=30)
    cate = models.OneToOneField(Cate,on_delete=models.CASCADE,verbose_name="商品类别")
    group_url = models.CharField('频道组的链接地址',max_length=100,default='http://jd.com')
    sequence = models.IntegerField('组内展示顺序',default=0)
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'cate_group'
        verbose_name_plural = '频道组'

class SPU(models.Model):
    name = models.CharField('spu名称',max_length=20)
    brand = models.ForeignKey(Brand,on_delete=models.PROTECT,verbose_name="所属品牌")
    category1 = models.ForeignKey(Cate,on_delete=models.PROTECT,related_name='cate1_spu',
                                  verbose_name='一级类别')
    category2 = models.ForeignKey(Cate,on_delete=models.PROTECT,
                                  related_name='cate2_spu',verbose_name='二级类别')
    category3 = models.ForeignKey(Cate,on_delete=models.PROTECT,
                                  related_name='cate3_spu',verbose_name="三级类别")
    comment_num = models.IntegerField('评论的数量',default=0)
    sales = models.IntegerField('销量',default=0)
    desc_detai = models.TextField('详细描述',default="")
    desc_pack = models.TextField('包装信息',default="")
    desc_service = models.TextField('服务信息',default="")

    def __str__(self):
        return self.name
    class Meta:
        db_table = 'spu'
        verbose_name_plural = "SPU"

class SKU(models.Model):
    name = models.CharField('商品sku',max_length=100)
    describe = models.CharField('商品描述',max_length=500)
    price = models.DecimalField('商品价格',max_digits=8,decimal_places=2)
    stock = models.IntegerField('商品库存',default=0)
    sales = models.IntegerField('商品销量',default=0)
    detail = models.TextField('详情描述',default="")
    image_default = models.CharField('默认图片',max_length=300)
    is_selling = models.BooleanField('是否在售',default=True)
    cate = models.ForeignKey(Cate, on_delete=models.PROTECT,verbose_name="所属类别")
    spu = models.ForeignKey(SPU,on_delete=models.PROTECT,verbose_name="SPU")
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'sku'
        verbose_name_plural = 'SKU'

#SPU规格模型类
class SPUSpecification(models.Model):
    name = models.CharField('规格名称',max_length=20)
    spu = models.ForeignKey(SPU,on_delete=models.CASCADE,verbose_name='商品SPU')
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'spu_specification'
        verbose_name_plural = 'SPU规格名称'


#定义规格选项值的模型类
class SpecificationOption(models.Model):
    value = models.CharField('选项值',max_length=30)
    spec = models.ForeignKey(SPUSpecification,on_delete=models.CASCADE,verbose_name='规格名称')
    def __str__(self):
        return "%s of %s"%(self.value,self.spec.name)
    class Meta:
        db_table = 'specification_option'
        verbose_name_plural = '规格选项值'

#sku的规格
class SKUSecification(models.Model):
    sku = models.ForeignKey(SKU,on_delete=models.CASCADE,verbose_name='sku商品')
    spec = models.ForeignKey(SPUSpecification,on_delete=models.CASCADE,verbose_name='规格名称')
    option = models.ForeignKey(SpecificationOption,on_delete=models.CASCADE,verbose_name="规格选项值")
    def __str__(self):
        return "%s: %s-%s"%(self.sku.name,self.spec.name,self.option.value)
    class Meta:
        db_table = 'sku_specification'
        verbose_name_plural = 'sku规格'


class SKUImage(models.Model):
    sku = models.ForeignKey(SKU,on_delete=models.CASCADE,verbose_name='商品sku',related_name='sku_images')
    images = models.ImageField('商品图片',max_length=300)
    def __str__(self):
        return "商品%s的图片"%self.sku.name
    class Meta:
        db_table = 'sku_image'
        verbose_name_plural = '商品sku图片'

